package com.mzpai.product.service.impl;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mzpai.product.exception.ProductResException;
import com.mzpai.product.exception.ProductResExceptionCode;
import com.mzpai.product.mapper.UserSuccessKilledMapper;
import com.mzpai.product.model.StoreSeckill;
import com.mzpai.product.model.UserSuccessKilled;
import com.mzpai.product.service.StoreSeckillService;
import com.mzpai.product.service.UserSuccessKilledService;
import com.mzpai.service.impl.BaseServiceImpl;

@Service
public class UserSuccessKilledServiceImpl extends BaseServiceImpl<UserSuccessKilled> implements UserSuccessKilledService {

	Logger logger = LoggerFactory.getLogger(UserSuccessKilledServiceImpl.class);

	@Autowired 
	private UserSuccessKilledMapper userSuccessKilledMapper;
	
	@Autowired 
	private StoreSeckillService storeSeckillService;

	@Override
	public UserSuccessKilled queryByPrimaryId(Integer id) {
		UserSuccessKilled userSuccessKilled=userSuccessKilledMapper.queryByPrimaryId(id);
		return userSuccessKilled;
	}

	@Override
	public void insertBatch(List<UserSuccessKilled> userSuccessKilledlList) {
		userSuccessKilledMapper.insertBatch(userSuccessKilledlList);
		
	}
	
	@Override
	public void saveSeckillSuccessOrder(List<UserSuccessKilled> userSuccessKilledList){
		
		/**
		 * 1.扣减秒杀商品库存
		 * 2.保存秒杀成功明细
		 */
		if(userSuccessKilledList!=null && !userSuccessKilledList.isEmpty()){
			for(UserSuccessKilled usk:userSuccessKilledList){
				Integer seckillId=usk.getSeckillId();
				Integer buyNum=usk.getBuyNum();
				StoreSeckill storeSeckill=storeSeckillService.queryByPrimaryId(seckillId);
				if(storeSeckill==null){
					throw new ProductResException(ProductResExceptionCode.SECKILL_PRODUCT_NOTEXIT.getCode(),ProductResExceptionCode.SECKILL_PRODUCT_NULL.getMessage());
				}
				Integer stock=storeSeckill.getStock();
				if(buyNum>stock){
					logger.error("msg1=扣减库存不足,,metod=saveSeckillSuccessOrder,,reqSeckillId={},reqBuyNum={},,stock={}",seckillId,buyNum,stock);
					throw new ProductResException(ProductResExceptionCode.SECKILL_PRODUCT_NOT_SUFFICIENT.getCode(),ProductResExceptionCode.SECKILL_PRODUCT_NOT_SUFFICIENT.getMessage());
				}
				
				Date stopTime=storeSeckill.getStopTime();
				int compareToAfter=stopTime.compareTo(new Date());
				if(compareToAfter<0){
					throw new ProductResException(ProductResExceptionCode.SECKILL_PRODUCT_TIME_END.getCode(),ProductResExceptionCode.SECKILL_PRODUCT_TIME_END.getMessage());
				}
				Integer lockStockNum=storeSeckill.getLockStockNum();
				StoreSeckill updateStoreSeckill=new StoreSeckill();
				updateStoreSeckill.setId(seckillId);
				Integer subStock=stock-buyNum;
				
				if(subStock<0){
					updateStoreSeckill.setStock(0);
				}else{
					updateStoreSeckill.setStock(subStock);
				}
				updateStoreSeckill.setLockStockNum(lockStockNum+buyNum);
				storeSeckillService.update(updateStoreSeckill);
			}
			//保存秒杀成功明细
			userSuccessKilledMapper.insertBatch(userSuccessKilledList);
		}
	}
	
	@Override
	public void updateUserSuccessKilledAndStoreSeckill(UserSuccessKilled userSuccessKilled,List<StoreSeckill> listStoreSeckill){
		if(listStoreSeckill!=null && !listStoreSeckill.isEmpty()){
			for(StoreSeckill storeSeckill:listStoreSeckill){
				storeSeckillService.update(storeSeckill);
			}
		}
		userSuccessKilledMapper.updateUserSuccessKilledByOrderId(userSuccessKilled);
	}

	@Override
	public Integer countTotalByUserIdAndSeckillId(Map<String, Object> map) {
		Integer total=userSuccessKilledMapper.countTotalByUserIdAndSeckillId(map);
		return total;
	}

	
}